package jvm.jiketime.z02;
// time java SafepointTestp
// 你还可以使用如下几个选项
// -XX:+PrintGC
// -XX:+PrintGCApplicationStoppedTime 
// -XX:+PrintSafepointStatistics
// -XX:+UseCountedLoopSafepoints

/**
 * 查看GC
 */
public class SafepointTest {
  static double sum = 0;
 
  public static void foo() {
    for (int i = 0; i < 0x77777777; i++) {
      sum += Math.sqrt(i);
    }
  }
 
  public static void bar() {
    for (int i = 0; i < 50_000_000; i++) {
      new Object().hashCode();
    }
  }
 
  public static void main(String[] args) {
    new Thread(SafepointTest::foo).start();
    new Thread(SafepointTest::bar).start();
  }
}



/*
    new Thread(SafepointTest::foo).start();

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>javac SafepointTest.java

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java SafepointTest

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintGC  SafepointTest

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintGCApplicationStoppedTime  SafepointTest
Total time for which application threads were stopped: 3.1569356 seconds, Stopping threads took: 3.1567961 seconds
Total time for which application threads were stopped: 0.0001135 seconds, Stopping threads took: 0.0000338 seconds

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintSafepointStatistics  SafepointTest
         vmop                    [threads: total initially_running wait_to_block]    [time: spin block sync cleanup vmop] page_trap_count
1.043: no vm operation                  [      11          1              1    ]      [  3142     0  3142     0     0    ]  0
4.185: EnableBiasedLocking              [      11          1              1    ]      [     0     0     0     0     0    ]  0
4.186: no vm operation                  [       9          0              1    ]      [     0     0     0     0     0    ]  0

Polling page always armed
EnableBiasedLocking                1
    0 VM operations coalesced during safepoint
Maximum sync time   3142 ms
Maximum vm operation time (except for Exit VM operation)      0 ms

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+UseCountedLoopSafepoints  SafepointTest

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>





    new Thread(SafepointTest::bar).start();

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>javac SafepointTest.java

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java SafepointTest

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintGC  SafepointTest
[GC (Allocation Failure)  131072K->1120K(500736K), 0.0011321 secs]
[GC (Allocation Failure)  132192K->1056K(500736K), 0.0019265 secs]
[GC (Allocation Failure)  132128K->1112K(500736K), 0.0014216 secs]
[GC (Allocation Failure)  132184K->1032K(500736K), 0.0010032 secs]
[GC (Allocation Failure)  132104K->960K(477184K), 0.0013455 secs]
[GC (Allocation Failure)  128960K->1064K(474624K), 0.0011273 secs]

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintGCApplicationStoppedTime  SafepointTest
Total time for which application threads were stopped: 0.0015010 seconds, Stopping threads took: 0.0000184 seconds
Total time for which application threads were stopped: 0.0013902 seconds, Stopping threads took: 0.0000201 seconds
Total time for which application threads were stopped: 0.0013571 seconds, Stopping threads took: 0.0000198 seconds
Total time for which application threads were stopped: 0.0006625 seconds, Stopping threads took: 0.0000343 seconds
Total time for which application threads were stopped: 0.0016520 seconds, Stopping threads took: 0.0000200 seconds
Total time for which application threads were stopped: 0.0012703 seconds, Stopping threads took: 0.0000194 seconds

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintSafepointStatistics  SafepointTest
         vmop                    [threads: total initially_running wait_to_block]    [time: spin block sync cleanup vmop] page_trap_count
0.354: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
0.545: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     0    ]  0
0.733: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
0.924: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
1.118: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
1.308: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
1.318: no vm operation                  [       8          0              0    ]      [     0     0     0     0     0    ]  0

Polling page always armed
ParallelGCFailedAllocation         6
    0 VM operations coalesced during safepoint
Maximum sync time      0 ms
Maximum vm operation time (except for Exit VM operation)      1 ms

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+UseCountedLoopSafepoints  SafepointTest

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>







    new Thread(SafepointTest::foo).start();
    new Thread(SafepointTest::bar).start();

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>javac SafepointTest.java

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java SafepointTest

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintGC  SafepointTest
[GC (Allocation Failure)  131072K->1088K(500736K), 0.0021863 secs]
[GC (Allocation Failure)  132160K->1088K(500736K), 0.0015698 secs]
[GC (Allocation Failure)  132160K->1088K(500736K), 0.0014776 secs]
[GC (Allocation Failure)  132160K->1032K(500736K), 0.0009871 secs]
[GC (Allocation Failure)  132104K->1088K(477696K), 0.0014640 secs]
[GC (Allocation Failure)  129088K->1008K(474112K), 0.0017209 secs]

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintGCApplicationStoppedTime  SafepointTest
Total time for which application threads were stopped: 3.8349526 seconds, Stopping threads took: 3.8334631 seconds
Total time for which application threads were stopped: 0.0013659 seconds, Stopping threads took: 0.0000202 seconds
Total time for which application threads were stopped: 0.0008645 seconds, Stopping threads took: 0.0000217 seconds
Total time for which application threads were stopped: 0.0008408 seconds, Stopping threads took: 0.0000180 seconds
Total time for which application threads were stopped: 0.0016370 seconds, Stopping threads took: 0.0000221 seconds
Total time for which application threads were stopped: 0.0009753 seconds, Stopping threads took: 0.0000192 seconds

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+PrintSafepointStatistics  SafepointTest
         vmop                    [threads: total initially_running wait_to_block]    [time: spin block sync cleanup vmop] page_trap_count
0.357: ParallelGCFailedAllocation       [      12          1              1    ]      [  3835     0  3835     0     2    ]  0
4.486: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
4.769: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
5.056: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
5.343: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     1    ]  0
5.628: ParallelGCFailedAllocation       [      11          0              0    ]      [     0     0     0     0     0    ]  0
5.648: no vm operation                  [       8          0              0    ]      [     0     0     0     0     0    ]  0

Polling page always armed
ParallelGCFailedAllocation         6
    1 VM operations coalesced during safepoint
Maximum sync time   3835 ms
Maximum vm operation time (except for Exit VM operation)      2 ms

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>java -XX:+UseCountedLoopSafepoints  SafepointTest

D:\workspace_koal\TestProject\src\jvm\jiketime\z02>

* */